package com.ke.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.ke.reggie.common.BaseContext;
import com.ke.reggie.common.R;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @BelongsProject: reggie_take_out
 * @BelongsPackage: com.ke.reggie.filter
 * @Author: ke_huifeng
 * @CreateTime: 2022-10-03  18:18
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@WebFilter(filterName = "loginCheckFilter", urlPatterns = "/*")
public class LoginCheckFilter implements Filter {
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    /*
     * @description:检查用户是否已经登录
     * @author: ke_huifeng
     * @date: 2022/10/3 18:30
     * @param: [servletRequest, servletResponse, filterChain]
     * @return: void
     **/
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        log.info("拦截到请求");
        String requestURI = request.getRequestURI();
        String[] urls = {
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/user/sendMsg",
                "/user/login"
        };

        Boolean check = check(urls, requestURI);

        if(check){
            filterChain.doFilter(request, response);
            return;
        }

        Object employee = request.getSession().getAttribute("employee");
        if(employee != null){
            log.info("本次请求{}不需要处理",requestURI);

            Object empId = request.getSession().getAttribute("employee");
            BaseContext.setCurrentId((Long) empId);

            filterChain.doFilter(request, response);
            return;
        }

        Object user = request.getSession().getAttribute("user");
        if(user != null){
            log.info("本次请求{}不需要处理",requestURI);

            Object userId = request.getSession().getAttribute("user");
            BaseContext.setCurrentId((Long) userId);

            filterChain.doFilter(request, response);
            return;
        }

        log.info("用户未登录");
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;
    }

    /*
     * @description:路径匹配
     * @author: ke_huifeng
     * @date: 2022/10/3 18:30
     * @param: [urls, requestURI]
     * @return: java.lang.Boolean
     **/
    public Boolean check(String[] urls, String requestURI){
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestURI);
            if (match){
                return true;
            }
        }
        return false;
    }
}
